package com.slipper.system.codeless.controller;

import com.slipper.system.codeless.bo.ScriptToolDetailBO;
import com.slipper.system.codeless.service.ScriptToolDetailService;
import com.slipper.core.tree.Node;
import com.slipper.core.tree.Tree;
import com.slipper.core.tree.TreeInfo;
import com.slipper.core.tree.TreeUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.slipper.system.codeless.bo.ScriptToolBO;
import com.slipper.system.codeless.service.ScriptToolService;
import com.slipper.constants.Constants;
import com.slipper.util.ResultUtil;
import com.slipper.util.Tool;

import java.util.List;

/**
 * 脚本工具表
 */
@RestController
@RequestMapping("/system/scriptTool")
public class ScriptToolController {
    @Resource
    private ScriptToolService scriptToolService;
    @Resource
    private ScriptToolDetailService scriptToolDetailService;
    
    @PostMapping(value = "/queryBy")
    public String queryBy(@RequestBody ScriptToolBO bo) {
        if(Tool.isBlank(bo.getId())) {
            return ResultUtil.failureResult("数据获取失败！");
        }
        return ResultUtil.successResult(scriptToolService.queryBy(bo.getId()));
    }

    @PostMapping(value = "/queryList")
    public String queryList(@RequestBody ScriptToolBO bo) {
        return ResultUtil.successResult(scriptToolService.queryList(bo));
    }
    
    @PostMapping(value = "/queryPage")
    public String queryPage(@RequestBody ScriptToolBO bo) {
        return ResultUtil.successResult(scriptToolService.queryPage(bo));
    }

    @PostMapping(value = "/queryTree")
    public String queryTree() {
        List<ScriptToolBO> folderList = scriptToolService.queryList(new ScriptToolBO());
        TreeInfo treeInfo = new TreeInfo("工具类");
        treeInfo.setRootId(Constants.ROOT_ID);
        treeInfo.setIdField("id");
        treeInfo.setPidField(null);
        treeInfo.setTextField("name");
        treeInfo.setSortField("ordinal");
        return ResultUtil.successResult(TreeUtil.buildTree(treeInfo, folderList, "asc"));
    }

    @PostMapping(value = "/queryWithDetailTree")
    public String queryWithDetailTree() {
        List<ScriptToolBO> folderList = scriptToolService.queryList(new ScriptToolBO());
        TreeInfo treeInfo = new TreeInfo("工具类");
        treeInfo.setRootId(Constants.ROOT_ID);
        treeInfo.setIdField("id");
        treeInfo.setPidField(null);
        treeInfo.setTextField("name");
        treeInfo.setSortField("ordinal");

        List<ScriptToolDetailBO> pageList = scriptToolDetailService.queryList(new ScriptToolDetailBO());
        TreeInfo pageTreeInfo = new TreeInfo("");
        pageTreeInfo.setIdField("id");
        pageTreeInfo.setPidField("toolId");
        pageTreeInfo.setTextField("name");
        pageTreeInfo.setSortField("ordinal");

        Node rootNode = TreeUtil.buildTree(
                "asc",
                new Tree(treeInfo, folderList),
                new Tree(pageTreeInfo, pageList));
        return ResultUtil.successResult(rootNode);
    }

    @PostMapping(value = "/save")
    public String save(@RequestBody ScriptToolBO bo) {
        bo.setOperateId(Constants.getCurrentUser().getEmployeeId());
        return ResultUtil.saveResult(scriptToolService.save(bo), bo);
    }

    @PostMapping(value = "/delete")
    public String delete(@RequestBody ScriptToolBO bo) {
        return ResultUtil.deleteResult(scriptToolService.delete(bo));
    }
}
